package com.estock.platform.http;

import org.apache.log4j.Logger;

import com.estock.platform.http.client.AbstractClientProxy;
import com.estock.platform.http.request.StockClientRequest;
import com.estock.platform.http.response.BaseResponse;
import com.estock.platform.http.response.JSONResponse;

public abstract class AbstractConnection {
	
	private static Logger logger = Logger.getLogger(AbstractConnection.class);
	
	@SuppressWarnings("unchecked")
	protected <T extends BaseResponse> T getResponse(Class<T> clazz, Object... params) throws Exception {
		StockClientRequest request = buildStockClientRequest(params);
		
		try {
			logger.info("send request using url: " + request.getLocationUri());
			JSONResponse response = 
					(JSONResponse) createStockClientProxy().request(request, clazz);
			return (T) response;
		} catch (Exception e) {
			System.out.println("Error in requesting:" + request.getLocationUri());
			throw new RuntimeException("can not get valid stock information.", e);
		}
	}
	
	protected abstract AbstractClientProxy createStockClientProxy();
	
	protected abstract StockClientRequest buildStockClientRequest(Object... params) throws Exception;
	
}
